//
//  MiZhiSendViewController.m
//  TerritoryShopkeeper
//
//  Created by 李小龙 on 2018/4/3.
//  Copyright © 2018年 ZDQK. All rights reserved.
//

#import "MiZhiSendViewController.h"

@interface MiZhiSendViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTF;
@property (weak, nonatomic) IBOutlet UITextField *numberTF;
@property (weak, nonatomic) IBOutlet UITextField *cardIDTF;
@property (weak, nonatomic) IBOutlet UITextField *mizhiTF;
@property (weak, nonatomic) IBOutlet UILabel *mizhiLabel;

@end

@implementation MiZhiSendViewController
#pragma mark - UIViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"米值发送";
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemAction:)];
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIColor ThemeColor] forKey:NSForegroundColorAttributeName];
    [rightItem setTitleTextAttributes:dic forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = rightItem;
    [self hiddenShadowImage:NO];
    
    if (TextIsNotNil([UserInfo shareInstance].user.mtel)) {
        [self.phoneNumberTF setText:[UserInfo shareInstance].user.mtel];
        //[self.phoneNumberTF setEnabled:YES];
    } else {
        [self.phoneNumberTF setEnabled:YES];
    }
    
    [self uploadUI];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    [self.view endEditing:YES];
}
#pragma mark - Action
- (void)uploadUI {
    if (TextIsNotNil([UserInfo shareInstance].user.mvalue)) {
        [self.mizhiLabel setText:[NSString stringWithFormat:@"我的米值：%@",[UserInfo shareInstance].user.mvalue]];
    } else {
        [self.mizhiLabel setText:@"我的米值：0"];
    }
}
- (void)rightBarButtonItemAction:(id)sender {
    if (!TextIsNotNil(self.phoneNumberTF.text)) {
        [MBProgressHUD myShowHUDAddedTo:self.view labelText:@"手机号不能为空" animated:YES];
        return;
    }
    if (!TextIsNotNil(self.numberTF.text)) {
        [MBProgressHUD myShowHUDAddedTo:self.view labelText:@"编号不能为空" animated:YES];
        return;
    }
    if (!TextIsNotNil(self.cardIDTF.text)) {
        [MBProgressHUD myShowHUDAddedTo:self.view labelText:@"身份证号不能为空" animated:YES];
        return;
    }
//    if (![LXLIsPhoneNumber judgeIdentityStringValid:self.cardIDTF.text]) {
//        [MBProgressHUD myShowHUDAddedTo:self.view labelText:@"请输入有效的身份证号" animated:YES];
//        return;
//    }
    if (!TextIsNotNil(self.mizhiTF.text)) {
        [MBProgressHUD myShowHUDAddedTo:self.view labelText:@"米值数额不能为空" animated:YES];
        return;
    }
    if (self.mizhiTF.text.integerValue > [UserInfo shareInstance].user.mvalue.integerValue) {
        [MBProgressHUD myShowHUDAddedTo:self.view labelText:@"米值数额不足" animated:YES];
        return;
    }
    if (self.mizhiTF.text.integerValue%10 != 0) {
        [MBProgressHUD myShowHUDAddedTo:self.view labelText:@"米值数额须是10的整数倍" animated:YES];
        return;
    }
    
    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
    [parameters setObject:@"getmyinfo" forKey:@"action"];
    [parameters setObject:self.numberTF.text forKey:@"hynumber"];
    [parameters setObject:self.phoneNumberTF.text forKey:@"mobile_phone"];
    [parameters setObject:self.cardIDTF.text forKey:@"cardno"];
    [parameters setObject:self.mizhiTF.text forKey:@"money_count"];
    
    [MBProgressHUD showHUDProgress:self.view animated:YES];
    __block typeof(self) blockSelf = self;
    [MYAFNetworking GETNormal:[NSString stringWithFormat:@"%@%@",BASE_JZG_URL,URL_MiZhi] parameters:parameters success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        /*
         100  接收成功
         101  编号错误
         102  手机错误
         103  身份证错误
         */
        [MBProgressHUD hideHUDForView:blockSelf.view animated:YES];
        NSString *string = [responseObject mj_JSONString];
        switch (string.integerValue) {
            case 100:
            {
                [MBProgressHUD myShowHUDAddedTo:blockSelf.view labelText:@"发送成功" animated:YES];
                [blockSelf uploadRemoveMiZhi];
            } break;
            case 101:
            {
                [MBProgressHUD myShowHUDAddedTo:blockSelf.view labelText:@"编号错误" animated:YES];
            } break;
            case 102:
            {
                [MBProgressHUD myShowHUDAddedTo:blockSelf.view labelText:@"手机号错误" animated:YES];
            } break;
            case 103:
            {
                [MBProgressHUD myShowHUDAddedTo:blockSelf.view labelText:@"身份证号错误" animated:YES];
            } break;
            default:
                break;
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:blockSelf.view animated:YES];
        [MBProgressHUD showError:blockSelf.view title:@"" code:@"" animated:YES];
    }];
}
#pragma mark - 网络请求
- (void)uploadRemoveMiZhi {
    [MBProgressHUD showHUDProgress:self.view animated:YES];
    __block typeof(self) blockSelf = self;
    [MYAFNetworking POST:URL_Remove_MiZhi parameters:@{@"mvalue":self.mizhiTF.text} success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:blockSelf.view animated:YES];
        if ([responseObject[@"code"] isEqualToString:@"200"]) {
            [[UserInfo shareInstance] updateUserInterfaceModel:^(BOOL success) {
                [blockSelf uploadUI];
                [blockSelf disMissSelf];
            }];
        } else {
            [MBProgressHUD showError:blockSelf.view title:responseObject[@"message"] code:responseObject[@"code"] animated:YES];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:blockSelf.view animated:YES];
        [MBProgressHUD showError:blockSelf.view title:@"" code:@"" animated:YES];
    }];
}
#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//    if (textField == self.mizhiTF) {
//        
//    }
    return YES;
}
#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
